package com.example.lyqwork.controller;

import com.example.lyqwork.pojo.ResponseMessage;
import com.example.lyqwork.pojo.User;
import com.example.lyqwork.pojo.dto.UserDto;
import com.example.lyqwork.pojo.dto.UserEditDto;
import com.example.lyqwork.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController //接口方法返回对象 转换成json文本
@RequestMapping("/user")
public class UserControoller {


    @Autowired
    IUserService userService;

    //增加
    //REST
    @PostMapping(consumes = "application/json")
    public ResponseMessage add(@Validated @RequestBody UserDto user){
        User re_user = userService.add(user);
        return ResponseMessage.success(re_user);
    }


    //查找
    @GetMapping(value = "/{userId}")
    public ResponseMessage find(@PathVariable Integer userId){
        User user = userService.getUser(userId);
        return ResponseMessage.success(user);
    }
    //修改
    @PutMapping(consumes = "application/json")
    public ResponseMessage update(@Validated @RequestBody UserEditDto user){
        User re_user = userService.update(user);
        return ResponseMessage.success(re_user);
    }
    //删除
    @DeleteMapping(value = "/{userId}",consumes = "application/json")
    public ResponseMessage deleted(@PathVariable Integer userId){
        userService.deleted(userId);
        return ResponseMessage.success(null);
    }


}
